package cn.b.sky.mes.hr.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import cn.b.sky.mes.hr.service.PerformanceService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.hr.dao.PerformanceDao;
import cn.b.sky.mes.hr.model.Performance;
import cn.b.sky.mes.hr.model.condition.PerformanceCondition;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.common.utils.DateUtils;

@Service("performanceService")
public class PerformanceServiceImpl implements PerformanceService {

  private static final Logger LOG = LoggerFactory.getLogger(PerformanceServiceImpl.class);

  @Resource
  private PerformanceDao performanceDao;

  @Override
  public List<Performance> getPerformancesByCondition(PerformanceCondition condition) {
    if (condition == null) {
      return null;
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("createDateTime");//人员编号排序
    }
    return performanceDao.selectPerformanceByCondition(condition);
  }

  @Override
  public int getPerformancesCountByCondition(PerformanceCondition condition) {
    if (condition == null) {
      return 0;
    }
    return performanceDao.selectPerformancesCountByCondition(condition);
  }

  @Override
  public Performance getPerformanceById(Long id, String siteCode) {
    if (null == id) {
      return null;
    }
    return performanceDao.selectPerformanceById(id, siteCode);
  }

  @Override
  public void savePerformance(Performance performance) {
    if (performance != null && !performance.isNullObject()) {
      performanceDao.insertPerformance(performance);
    }

  }

  @Override
  public void modifyPerformance(Performance performance) {
    if (performance != null) {
      performanceDao.updatePerformance(performance);
    }

  }

  @Override
  public String deletePerformanceById(Long id) {
    if (id == null) {
      return null;
    }
    String message = null;
    try {
      performanceDao.deletePerformance(id);
    } catch (Exception e) {
      LOG.error("{}", e);
      String errorMsg = e.getMessage();
      if ((DatabaseUtils.isConstraintsError(errorMsg))) {
        // 外键删除已存在提示
        message = SystemConstant.DATABASE_OPERATION_FK;
      } else {
        message = SystemConstant.DWZ_CODE_ERROR_MESSAGE;
      }
    }
    return message;
  }

  @Override
  public void saveBatchPerformance(Performance performance) {
    //  Auto-generated method stub

  }

  @Override
  public boolean isExsitsPerformance(Performance performance) {
    if (StringUtils.isBlank(performance.getPersonnelCode())) {
      return false;
    }
    Performance p = performanceDao.selectPerformanceByPersonnel(performance.getId(), performance.getPersonnelCode(),
        performance.getSiteCode(), DateUtils.getDateUtils().getDateStr(performance.getStartDatetime()), DateUtils
            .getDateUtils().getDateStr(performance.getEndDatetime()));
    if (p != null && p.getId() != null) {
      return true;
    }
    return false;
  }

  @Override
  public Performance getMinStartTimePerformance(Performance performance) {
    List<Performance> performances = performanceDao.selectPerformanceByPersonnelAndTheDate(performance.getId(),
        performance.getPersonnelCode(), performance.getSiteCode(), DateUtils.getDateUtils(DateUtils.YYYY_MM_DD)
            .getDateStr(performance.getTheDate()));
    if (performances != null && performances.size() > 0) {
      return performances.get(0);
    } else {
      return performance;
    }
  }

  @Override
  public List<Performance> getPerformanceBypersonnelAndTheDate(Performance performance) {
    return performanceDao.selectPerformanceByPersonnelAndTheDate(performance.getId(), performance.getPersonnelCode(),
        performance.getSiteCode(), DateUtils.getDateUtils(DateUtils.YYYY_MM_DD).getDateStr(performance.getTheDate()));
  }

  @Override
  public List<Performance> getPerformanceListByCondition(PerformanceCondition condition) {
    return performanceDao.getPerformanceListByCondition(condition);
  }

  @Override
  public List<Map<String, Object>> selectAllPerformanceReportMapList(PerformanceCondition condition) {
    return performanceDao.selectAllPerformanceReportMapList(condition);
  }

  @Override
  public int getPerformanceCountByCondition(PerformanceCondition condition) {
    return performanceDao.getPerformanceCountByCondition(condition);
  }

  @Override
  public List<Performance> getPerformanceBypersonnelAndPeriod(PerformanceCondition condition) {
    return performanceDao.selectPerformanceByPersonnelAndPeriod(condition);
  }
}
